//
//  MyTableViewController.swift
//  RoundCatPlay2
//
//  Created by 姜澎 on 2019/10/24.
//  Copyright © 2019 jiangpeng. All rights reserved.
//

import UIKit

class MyViewController: UIViewController {
    
    let tvcDataSource = [
        [["titleName":"订单","imageNmae":"订单"]],
        [["titleName":"技能","imageNmae":"技能"],["titleName":"接单设置","imageNmae":"接单设置"],["titleName":"联系客服","imageNmae":"联系客服"]],
        [["titleName":"钱包","imageNmae":"钱包"],["titleName":"系统设置","imageNmae":"系统设置"]],
        [["titleName":"意见","imageNmae":"意见"]]
    ]
    
    
    //    左侧设置按钮
    private lazy var setBtu:UIButton = {
        var btu = UIButton(type: .custom)
        
        var image = UIImage(named: "setBtu")
        btu.imageView?.contentMode = .scaleAspectFill
        btu.setImage(image, for: .normal)
        btu.setImage(image, for: .highlighted)
        btu.addTarget(self, action: #selector(setingAndTitleImageFunc), for: .touchUpInside)
        btu.frame = CGRect(origin: .zero, size: CGSize(width: 26, height: 25))
        btu.tag = 1
        return btu
    }()
    //   我的  UIlbale
    private lazy var myLable: UILabel = {
        let l = UILabel()
        l.text = "我的"
        l.textColor = UIColor.white
        l.font = UIFont.systemFont(ofSize: 23)
        return l
    }()
    //    头像
    private lazy var titleImageBut: UIButton = {
        let titileImageBut = UIButton()
        titileImageBut.setImage(UIImage(named:"组12"), for: .normal)
        titileImageBut.setImage(UIImage(named:"组12"), for: .highlighted)
        titileImageBut.layer.cornerRadius = 105 / 2
        titileImageBut.addTarget(self, action: #selector(setingAndTitleImageFunc), for: .touchUpInside)
        titileImageBut.tag = 2
        return titileImageBut
    }()
    
    //    名字
    private lazy var nameLable: UILabel = {
        let l = UILabel()
        l.text = "圆猫陪玩"
        l.font = UIFont.systemFont(ofSize: 20)
        l.textColor = .white
        l.isUserInteractionEnabled = true
        l.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(nameLableAndTelLableClick)))
        l.tag = 3
        return l
    }()
    //    电话
    private lazy var telLable: UILabel = {
        let l = UILabel()
        l.text = "15556318803"
        l.font = UIFont.systemFont(ofSize: 18)
        l.textColor = .white
        l.isUserInteractionEnabled = true
        l.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(nameLableAndTelLableClick)))
        l.tag = 4
        return l
    }()
    
    private lazy var tvc: UITableView = {
        var tvc = UITableView()
        tvc.delegate = self
        tvc.dataSource = self
        tvc.backgroundColor = UIColor.clear
        tvc.separatorStyle = .none
        tvc.showsVerticalScrollIndicator = false
        
        return tvc
    }()
    
    private lazy var rz:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFit
        v.image = UIImage(named: "认证")
        return v
    }()
    
    private lazy var backImage: UIImageView = {
        let i = UIImageView()
        i.image = UIImage(named: "组13")
        return i
    }()
    
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
//        self.view.addSubview(UIImageView.init(image: UIImage(named: "组13")))
//
//        //        设置透明
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()

        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.setBtu)

        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.myLable)
        
        setUI()
        
        
    }
    
}

// tableVC 代理 和 数据源
extension MyViewController: UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.tvcDataSource.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tvcDataSource[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "mytcell")
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: "mytcell")
        }
        
        let arr = self.tvcDataSource[indexPath.section]
        let d = arr[indexPath.row]
        let m = MyCellModel(titleName: d["titleName"]!, imageName: d["imageNmae"]!)
        cell?.textLabel?.text = m.titleName
        cell?.imageView?.image = UIImage(named: m.imageName)
        
        cell?.layer.cornerRadius = 7
        cell?.backgroundColor = .clear // 清除cell的颜色 是为了内边距
        cell?.contentView.backgroundColor = .white
        cell?.contentView.layer.cornerRadius = 7
        cell?.selectionStyle = .none
        cell?.accessoryType = .disclosureIndicator
        
         cell?.contentView.snp.makeConstraints({ (make) in
            make.top.equalTo(0)
            make.bottom.equalTo(-2)
            make.left.equalTo(0)
            make.right.equalTo(0)
        })
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    //    组视图间距 两个一起使用 因为没有组视图 要创建一个
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
}

//  MARK 点击事件
extension MyViewController {
    //    点击设置按钮跳转 和 点击头像跳转
    @objc func setingAndTitleImageFunc(but:UIButton) {
        if but.tag == 1 {
            self.navigationController?.pushViewController(MySetingViewController(), animated: true)
        }else {
            print("iamgeTitle")
        }
        
    }
    
    //    nameLable点击跳转
    @objc func nameLableAndTelLableClick(tgr:UITapGestureRecognizer){
        guard let lable = tgr.view as? UILabel else {return}
        if lable.tag == 3 {
            print("nameL")
        }else {
            print("telL")
        }
    }
}


extension MyViewController {
    
    func setUI(){
        view.addSubview(self.backImage)
        view.addSubview(self.titleImageBut)
        view.addSubview(self.nameLable)
        view.addSubview(self.telLable)
        view.addSubview(self.rz)
        view.addSubview(self.tvc)
        
        setTitleLImageButFrame()
        setNameLabelFrame()
        setTelLableFuncFrame()
        setRZImageFuncFrame()
        setTVCFrameFunc()
        setBackImage()
    }
    
    func setTitleLImageButFrame() {
        self.titleImageBut.snp.makeConstraints { (make) in
            make.top.equalTo(100)
            make.left.equalTo(12)
            make.width.equalTo(105)
            make.height.equalTo(105)
        }
    }
    
    func setNameLabelFrame() {
        self.nameLable.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleImageBut.snp_top).offset(20)
            make.left.equalTo(self.titleImageBut.snp_right).offset(10)
//            make.width.equalTo(90)
//            make.height.equalTo(26)
        }
    }
    
    func setTelLableFuncFrame(){
        self.telLable.snp.makeConstraints { (make) in
            make.top.equalTo(self.nameLable.snp_bottom).offset(10)
            make.left.equalTo(self.nameLable.snp_left)
            make.width.equalTo(155)
            make.height.equalTo(18)
        }
    }
    
    func setRZImageFuncFrame(){
        self.rz.snp.makeConstraints { (make) in
            make.top.equalTo(self.nameLable.snp_top).offset(2)
            make.left.equalTo(self.nameLable.snp_right).offset(5)
//            make.width.equalTo(70)
//            make.height.equalTo(20)
        }
    }
    
    func setTVCFrameFunc() {
        self.tvc.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleImageBut.snp_top).offset(110)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(0)
        }
    }
    
    func setBackImage() {
        self.backImage.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.left.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
}


